package com.zhaowei.codingintervierguide.stackandqueue;

import org.omg.PortableInterceptor.INACTIVE;

import java.util.Stack;

class StacksToQueue {
    private Stack<Integer> stackLeft;
    private Stack<Integer> stackRight;

    public StacksToQueue() {
        this.stackLeft = new Stack<>();
        this.stackRight = new Stack<>();
    }

    public void add(Integer element) {
        if (this.stackRight.empty()) {
            this.stackRight.push(element);
        } else {
            while (!this.stackRight.empty()) {
                this.stackLeft.push(this.stackRight.pop());
            }
            this.stackLeft.push(element);
            while (!this.stackLeft.isEmpty()) {
                this.stackRight.push(this.stackLeft.pop());
            }
        }
    }

    public Integer poll() {
        if (!this.stackRight.isEmpty()) {
            return this.stackRight.pop();
        } else return null;
    }

    public Integer peek() {
        if (!this.stackRight.isEmpty()) {
            return this.stackRight.peek();
        } else return null;
    }
}

public class TwoStacksTmplementQueue {

    public static void main(String[] args) {
        StacksToQueue stacksToQueue = new StacksToQueue();
        stacksToQueue.add(1);
        stacksToQueue.add(4);
        stacksToQueue.add(3);

        System.out.println(stacksToQueue.peek());
        System.out.println(stacksToQueue.poll());
        System.out.println(stacksToQueue.poll());
    }
}
